package com.dense.kuiniu.bookborrow;

import android.app.Application;
import android.content.Context;
import android.os.Environment;

import com.android.serialport.SerialPortManager;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.dense.kuiniu.bookborrow.base.SpKey;
import com.dense.kuiniu.bookborrow.base.Url;
import com.dense.kuiniu.bookborrow.net.SocketChecker;
import com.dense.kuiniu.bookborrow.receiver.SystemTTS;
import com.dense.kuiniu.bookborrow.xjSetting.manager.PingBaoManager;
import com.dense.kuiniu.bookborrow.xjSetting.manager.YuyinUtils;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import rxhttp.RxHttp;

/**
 * @author Administrator
 * @date 2020/8/6
 */
public class App extends Application {
    public static final String APP_ID = "704b74d393";
    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        Utils.init(this);
        ToastUtils.setBgResource(R.drawable.shape_bg_toast);
        initTTS();
        RxHttp.init(new OkHttpClient.Builder()
                .connectTimeout(3, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.SECONDS)
                .hostnameVerifier((hostname, session) -> true) //忽略host验证
                .build(), true);
        String baseUrl = SPUtils.getInstance().getString(SpKey.KEY_BASE_URL);
        if (!baseUrl.isEmpty()) {
            Url.baseUrl = baseUrl;
        }
        mContext = this;
        initBugLy();
        SerialPortManager.getInstance().openRFIDDevice();
        PingBaoManager.getInstance().initData();
        YuyinUtils.getInstall();
        SocketChecker.getInstance().startCheckServer();
    }

    private void initBugLy() {
        //自动初始化
        Beta.autoInit = true;
        //自动升级
        Beta.autoCheckUpgrade = true;
        //升级周期为60s
        Beta.initDelay = 1000;
        Beta.largeIconId = R.mipmap.ic_launcher;
        Beta.smallIconId = R.mipmap.ic_launcher;
        Beta.defaultBannerId = R.mipmap.ic_launcher;
        Beta.storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        Beta.showInterruptedStrategy = false;
        Bugly.init(getApplicationContext(), APP_ID, true);
    }

    private void initTTS() {
        SystemTTS.getInstance().init(this);
    }

    public static Context getContext() {
        return mContext;
    }
}